package com.example.springcloudribbon.config;

import com.example.springcloud.config.Common;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/6/13 21:48
 * @since JDK 1.8
 */
@Configuration
//导入swagger
@Common
public class RestTemplateConfig {

    @Bean
    //让restTemplate有负载均衡功能  ribbon
    @LoadBalanced
    public RestTemplate getRestTemplate() {
        HttpComponentsClientHttpRequestFactory httpRequestFactory =  new HttpComponentsClientHttpRequestFactory();
        httpRequestFactory.setReadTimeout(5000);
        httpRequestFactory.setConnectTimeout(5000);
        return new RestTemplate(httpRequestFactory);
    }
}
